import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (_) => FloatingButtonProvider(),
      child: MyApp(),
    ),
  );
}

class FloatingButtonProvider extends ChangeNotifier {
  Offset _position = Offset(100, 100); // Initial position

  Offset get position => _position;

  void updatePosition(Offset newPosition) {
    _position = newPosition;
    notifyListeners();
  }
}

class PersistentFloatingButton extends StatelessWidget {
  const PersistentFloatingButton({super.key});

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<FloatingButtonProvider>(context);

    return Positioned(
      left: provider.position.dx,
      top: provider.position.dy,
      child: GestureDetector(
        onPanUpdate: (details) {
          provider.updatePosition(provider.position + details.delta);
        },
        child: FloatingActionButton(
          onPressed: () {
            print("FAB Pressed");
          },
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}


class FirstPage extends StatelessWidget {
  const FirstPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('First Page')),
      body: Stack(
        children: [
          Center(child: Text('First Page Content')),
          PersistentFloatingButton(),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (_) => SecondPage()),
          );
        },
        child: Icon(Icons.navigate_next),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: FirstPage());
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Second Page')),
      body: Stack(
        children: [
          Center(child: Text('Second Page Content')),
          PersistentFloatingButton(),
        ],
      ),
    );
  }
}
